package com.wang.common.exception;

import com.wang.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author wang
 * @Date 2024/2/14 19:23
 * @PackageName:com.atguigu.system.exception
 * @ClassName: GlobalExceptionHandler
 * @Description: TODO
 * @Version 1.0
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e) {
        // 记录异常信息到日志系统
        log.error("全局异常处理", e);
        // 返回更具体的错误信息
        return Result.fail().message("执行了全局异常处理: " + e.getMessage());
    }

    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result error (ArithmeticException e){
        log.error("特殊异常处理", e);
        return Result.fail().message("执行了特定异常处理"+e.getMessage());
    }

}
